package com.secneo.appshare.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.util.Log;

import com.secneo.appshare.activities.AppshareAlarmActivity;
import com.secneo.appshare.api.RemoteApi.PushMessage;
import com.secneo.appshare.api.impl.RemoteApiImpl;
import com.secneo.appshare.common.AppSharePreference;
import com.secneo.appshare.common.Constants;
import com.secneo.appshare.common.ReadLogCat;


/**
 * @author gaowenting
 *
 */
public class AppShareService extends Service {
SimpleDateFormat df = new SimpleDateFormat("HH:mm");
Date d = null;
	private Timer timer;
	private Timer timers;
	
	private String lastPushedTime="";
	
	private TimerTask updateTask = new TimerTask() {
		@Override
		public void run() {		
			try {
				ReadLogCat.getLog(AppShareService.this);
			} catch (Throwable t) {
				t.printStackTrace();
			}
		}
	};
	private TimerTask pushTask = new TimerTask() {
		@Override
		public void run() {		
			try {
				Log.d("Push", "Run");
				if(AppSharePreference.isAppSharePushStatus(AppShareService.this))
				{
					equalsTime();
				}
			} catch (Throwable t) {
				t.printStackTrace();
			}
		}
	};
	
	
	@Override
	public IBinder onBind(Intent intent) {
		Log.d("MyTag", "AppShareService onBind");
		return null;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		Log.d("MyTag", "AppShareService onCreate");
		timer = new Timer("APPshareTimer");
		timer.schedule(updateTask, 60*1000L, 60*1000L);
		timers = new Timer("APPshareTimer");
		timers.schedule(pushTask, 60*1000L, 60*1000L);
		//每分钟检查一次
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		Log.d("MyTag", "AppShareService onDestroy");
		startService(new Intent(getApplicationContext(), AppShareService.class));
	}
	
	@Override
		public void onStart(Intent intent, int startId) {
			// TODO Auto-generated method stub
			super.onStart(intent, startId);
			
			Log.d("MyTag", "AppShareService onStart");
		}
	@Override
		public boolean onUnbind(Intent intent) {
			// TODO Auto-generated method stub
		Log.d("MyTag", "AppShareService onUnbind");
			return super.onUnbind(intent);
		}
	
	// 判断时间是否相等
	public void equalsTime() {
		
		Log.e("Push", "equalsTime");

		Date riqi = new Date();
		
		int year = riqi.getYear();
		int month = riqi.getMonth();
		int date = riqi.getDate();
		String dateString = year + "Y" + month+"M"+date+"D" ;
		
		int h = riqi.getHours();
		int m = riqi.getMinutes();
		int backupM = m;
		if(0==m){
			backupM++;
		}
		else{
			backupM--;
		}
		
		int s = riqi.getSeconds();
		String shi = h + ":" + m;
		String backupShi = h + ":" + backupM;
		
//		System.out.println("now time="+shi);
		String pushTime1=Constants.pushFirstTime;
		String pushTime2=Constants.pushSecondTime;
		String pushTime3=Constants.pushThirdTime;

		String thisPushedTime="";
		

		Log.e("Push", shi);
		Log.e("Push", backupShi);
		
		if (shi.equals(pushTime1)||shi.equals(pushTime2)||shi.equals(pushTime3) 
				|| backupShi.equals(pushTime1)||backupShi.equals(pushTime2)||backupShi.equals(pushTime3) ){
			
			Log.d("MyTag", "Shot Push Time");
			
			if(shi.equals(pushTime1) || backupShi.equals(pushTime1) ){
				Log.d("MyTag", pushTime1);
				thisPushedTime = dateString+"Time01";
			}
			else if(shi.equals(pushTime2) || backupShi.equals(pushTime2) ){
				Log.d("MyTag", pushTime2);
				thisPushedTime = dateString+"Time02";
			}
			else if(shi.equals(pushTime3) || backupShi.equals(pushTime3) ){
				Log.d("MyTag", pushTime3);
				thisPushedTime = dateString+"Time03";
			}
			
			if(thisPushedTime.equals(lastPushedTime)){
				//检查是否重复推送，避免连续两分钟都判断成功导致弹出两次对话框
				//当前一分钟的判断由于机器原因延迟可能会导致判断落到此分钟内
				Log.d("MyTag", "Ignore Duplicated Push");
				return;
			}

			Log.d("MyTag", thisPushedTime);
			Log.d("MyTag", "Push Operation!");
			lastPushedTime = thisPushedTime;
			
			checkAppPush();
		}
	}

	// get push data
	public void checkAppPush() {
		// TODO Auto-generated method stub

		Thread appPushThread = new Thread() {
			public void run() {
				Log.e("Push", "start Thread");
				RemoteApiImpl remoteApi = new RemoteApiImpl();
				PushMessage pushMessage = remoteApi
						.pushMessage(AppShareService.this);
				try {
					if (pushMessage != null) {
						Intent in = new Intent();
						in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
						in.putExtra("pushMessage", pushMessage.pushMessage);
						in.putExtra("appName", pushMessage.appName);
						in.putExtra("appIcon", pushMessage.appIcon);
						in.putExtra("packageName",pushMessage.appPackageName);
						in.putExtra("pushTitle",pushMessage.pushTitle);
						in.putExtra("pushYOBOUrl",pushMessage.pushYOBOUrl);
						in.putExtra("pushFirstButton",pushMessage.pushFirstButton);
						in.putExtra("pushSecondButton",pushMessage.pushSecondButton);
						in.putExtra("pushStatus",pushMessage.pushStatus);
						
						Log.e("Push", "pushMessage.appName:"+pushMessage.appName);
						Log.e("Push", "pushMessage.appIcon:"+pushMessage.appIcon);
						Log.e("Push", "pushMessage.appPackageName:"+pushMessage.appPackageName);
						
						in.setClass(AppShareService.this,
								AppshareAlarmActivity.class);
						AppShareService.this.startActivity(in);
					}
				} catch (Exception e) {
					Log.e("Push", e.getMessage());
					e.printStackTrace();
				}
			}
		};
		appPushThread.start();
		Log.e("Push", "appPushThread.start()");

	}
}
